package com.banmao.youZone.entity;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.banmao.youZone.config.EmptyStringListDeserializer;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName("task_trigger")
public class TaskTrigger {

    @TableId(value = "id", type = IdType.ASSIGN_UUID)
    private String id;

    @TableField("userId")
    private Integer userId;

    @TableField(exist = false)
    private String userName;

    @TableField("`index`")
    private Integer idx;

    @TableField(exist = false)
    @JsonDeserialize(using = EmptyStringListDeserializer.class)
    private List<String> timeSection;

    @TableField("timeSectionBegin")
    private String timeSectionBegin;

    @TableField("timeSectionEnd")
    private String timeSectionEnd;

    @TableField("precisionDateTime")
    private String precisionDateTime;

    @TableField("type")
    private Integer type;

    @TableField("mode")
    private Integer mode;

    @TableField("open")
    private Boolean open;

    @TableField("task_type")
    private String taskType;

    @TableField("email_title")
    private String emailTitle;

    @TableField("email_content")
    private String emailContent;

    // 预计执行时间
    @TableField(exist = false)
    private DateTime predictExecuteTime;

    public static TaskTrigger getInstance(int idx, Integer userId, List<String> timeSection) {
        return TaskTrigger.builder()
                .userId(userId)
                .idx(idx)
                .type(1)
                .mode(1)
                .open(true)
                .timeSection(timeSection)
                .build();
    }

    public void handleTaskTrigger(Integer userId, String userName) {
        Integer type = this.getType();
        if (type == null) {
            throw new IllegalArgumentException("type is null");
        }
        if (type == 1) {
            List<String> timeSection = this.getTimeSection();
            if (timeSection == null || timeSection.size() < 2) {
                throw new IllegalArgumentException("timeSection is null or not enough");
            }
            if (this.getMode() == null) {
                throw new IllegalArgumentException("mode is null");
            }
            String timeSectionBegin = timeSection.get(0);
            String timeSectionEnd = timeSection.get(1);
            DateTime beginTime = DateUtil.parse(timeSectionBegin, "HH:mm");
            DateTime endTime = DateUtil.parse(timeSectionEnd, "HH:mm");
            if (beginTime.equals(endTime)) {
                throw new IllegalArgumentException(timeSectionBegin + "区间时间不能相同");
            }
            this.setTimeSectionBegin(timeSectionBegin);
            this.setTimeSectionEnd(timeSectionEnd);
            log.info("[submitTasks]用户:[{}]{},type:{},beginTime:{},endTime:{}",
                    userId, userName, type, beginTime, endTime);
        } else if (type == 2) {
            String precisionDateTime = this.getPrecisionDateTime();
            DateTime precisTime = DateUtil.parse(precisionDateTime, DatePattern.NORM_DATETIME_MINUTE_FORMAT);
            log.info("[submitTasks]用户:[{}]{},type:{},precisTime:{}",
                    userId, userName, type, precisTime);
            if (this.getOpen() && DateUtil.compare(precisTime, DateUtil.date()) <= 0) {
                throw new IllegalArgumentException("时间:" + precisTime + " 比当前时间早，没必要配置");
            }
        } else {
            throw new IllegalArgumentException("Illegal type");
        }
        this.setUserId(userId);
        this.setUserName(userName);
    }

}
